package com.jn.algorithm.day024.question77;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

/**
 * @author 江南大学1033190417
 * @date 2022/5/15 9:47
 */
public class Solution {
    public List<List<Integer>> combine(int n, int k) {
        List<List<Integer>> res = new ArrayList<>();
        boolean[] used = new boolean[n + 1];
        Deque<Integer> path = new ArrayDeque<>();
        dfs(res, path, used, n, k, 0);
        return res;
    }

    //表示深度限制 depth当前深度
    private void dfs(List<List<Integer>> res, Deque<Integer> path, boolean[] used, int n, int k, int depth) {
        if (depth == k) {
            res.add(new ArrayList<>(path));
            return;
        }
        for (int i = 1; i <= n; i++) {
            if (used[i]) {
                continue;
            }
            if (path.isEmpty()) {
                path.addLast(i);
                used[i] = true;
                dfs(res, path, used, n, k, depth + 1);
                path.pollLast();
                used[i] = false;
            }else {
                if (path.peekLast()<i){
                    path.addLast(i);
                    used[i] = true;
                    dfs(res, path, used, n, k, depth + 1);
                    path.pollLast();
                    used[i] = false;
                }
            }
        }
    }

    public static void main(String[] args) {
        Solution solution=new Solution();
        System.out.println(solution.combine(4, 2));
    }
}
